package com.sskj.jgxt.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sskj.core.exception.MException;
import com.sskj.jgxt.RestParamVO;
import com.sskj.jgxt.RestQueryVO;
import com.sskj.jgxt.eng.domain.entity.DingJianDuEng;
import com.sskj.jgxt.jd.vo.FileInfo;
import com.sskj.jgxt.param.EngQueryParam;
import com.sskj.utils.HttpUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping(value = "/wechat/eng", method = { RequestMethod.POST, RequestMethod.GET })
@Api(value = "工程信息", description = "工程信息")
public class WeChartEngServiceImpl {

	protected final Log logger = LogFactory.getLog(WeChartEngServiceImpl.class);

	@Value("${sskj.conf.restUrl}")
	private String restUrl;

	

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/page", method = RequestMethod.GET)
	@ApiOperation(value = "查询工程", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "keyword", value = "关键字", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "jdfzr", value = "监督组成员", paramType = "form", dataType = "string"),					
			@ApiImplicitParam(name = "pageNumber", value = "页号", paramType = "form", dataType = "int"),
			@ApiImplicitParam(name = "pageSize", value = "页的大小", paramType = "form", dataType = "int") })
	public List<DingJianDuEng> queryPage(@ApiIgnore EngQueryParam engQueryParam, @ApiIgnore Pageable pageable)
			throws MException {

		List<DingJianDuEng> result = new ArrayList<DingJianDuEng>();

		RestQueryVO data = new RestQueryVO();

		data.setPage(pageable.getPageNumber() - 1);
		data.setPageSize(pageable.getPageSize());
		data.setNameQueryName("wx.jiandueng");

		RestParamVO param = new RestParamVO();
		param.setPn("engName");
		param.setValue(engQueryParam.getKeyword());
		data.getParams().add(param);

		RestParamVO param2 = new RestParamVO();
		param2.setPn("jdfzr");
		param2.setValue(engQueryParam.getJdfzr());
	
		data.getParams().add(param2);


		try {
						
			JSONObject obj = HttpUtils.httpPost(restUrl + "engcontrol/queryJDEngListPage", data);
			if (obj != null) {
				result = JSON.parseArray(obj.getString("resultls"), DingJianDuEng.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new MException("request_timeout");
		}

		return result;

	}
	
	/***
	 * 工程信息
	 * 
	 * @return
	 * @throws IOException
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/engbyid", method = RequestMethod.GET)
	@ApiOperation(value = "工程信息", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "code", value = "ID",
			paramType = "query", dataType = "string") })
	public JSONObject engById(String code, HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		JSONObject obj = new JSONObject();	
		if(!StringUtils.isEmpty(code))
		{
			obj = 
					HttpUtils.httpGetOne(
					restUrl + "engcontrol/engbyid/" + code,
					null);				
		}
		return obj;
	}	

}
